# 使用蓝图
import time
from flask import Blueprint, request, render_template, session, g, current_app
from .models import *
from .exts import cache

blue = Blueprint('user',__name__)
blue2 = Blueprint('prodect',__name__)

@blue.route('/')
@cache.cached(timeout = 20)  # 给缓存函数加一个缓存20秒
def index():
    print('index')
    print('index视图函数中：', g.star)
    time.sleep(5)
    return 'index'

# 钩子函数，before_request:每一次请求之前访问
@blue.before_request
def before():
    print('before_request')

    # print(request.path)
    # print(request.method)
    # print(request.remote_addr) # 客户端地址

    # 简单的反爬
    # print(request.user_agent)
    # if 'python' in request.user_agent.string:
    #     return '您正在使用python爬虫，再见'

    # 针对ip做简单的反爬虫
    ip = request.remote_addr
    if cache.get(ip):
        return '小伙子，别爬了'
    else:
        cache.set(ip,'value',timeout=1)

    g.star = '杰伦'
    print(g.star)
    print(current_app)
    print(current_app.config)

# static和templates
@blue.route('/template/')
def template():
    return render_template('template.html')


